/**
 * Copyright (C) 2005-2009 Alfresco Software Limited.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

 * As a special exception to the terms and conditions of version 2.0 of 
 * the GPL, you may redistribute this Program in connection with Free/Libre 
 * and Open Source Software ("FLOSS") applications as described in Alfresco's 
 * FLOSS exception.  You should have recieved a copy of the text describing 
 * the FLOSS exception, and it is also available here: 
 * http://www.alfresco.com/legal/licensing
 */
 
if (typeof GWI == "undefined" || !GWI){
	var GWI = {};
} 
GWI.dashlet = GWI.dashlet || {};  

/**
 * Dashboard Site Tags component.
 * 
 * @namespace GWI
 * @class GWI.dashlet.RunChallenge
 */
(function()
{
	/**
	 * YUI Library aliases
	 */
	var 	Dom = YAHOO.util.Dom,
			Event = YAHOO.util.Event;

	/**
	 * Alfresco Slingshot aliases
	 */
	var 	$html = Alfresco.util.encodeHTML,
			$combine = Alfresco.util.combinePaths;

	/**
	 * RunRanking view constructor.
	 * 
	 * @param {String} htmlId The HTML id of the parent element
	 * @return {GWI.dashlet.RunRanking} The new component instance
	 * @constructor
	 */
	GWI.dashlet.RunRanking = function GWI_RunRanking_constructor(htmlId){
		return GWI.dashlet.RunRanking.superclass.constructor.call(this, "GWI.dashlet.RunRanking", htmlId);
	};

	/**
	 * Extend from Alfresco.component.Base and add class implementation
	 */
	YAHOO.extend(GWI.dashlet.RunRanking, Alfresco.component.Base,
	{
		/**
		 * Object container for initialization options
		 *
		 * @property options
		 * @type object
		 */
		options:
		{

		},
      
		/**
		 * Dom containing the run ranking
		 * @property options
		 * @type object
		 */
		bodyContainer : null,
		
		/**
		 * Publisher
		 * @property publisher
		 * @type object
		 */
		publisher : null,
      
		/**
		 * Fired by YUI when parent element is available for scripting
		 * @method onReady
		 */
		onReady: function GWI_RunRanking_onReady()
		{
			var me = this;
         
			// The body container
			this.bodyContainer = Dom.get(this.id);
			
			//Init publisher
			this.publisher = new function () {
				this.sendMessage = function (msg) {
					YAHOO.util.Event.publish(
						"SHOW_USER_DETAILS_CHANNEL",
						{message: msg});
				},
				this.showHistory = function (msg) {
					YAHOO.util.Event.publish(
						"SHOW_HISTORY_CHANNEL",
						{message: msg});
				}
			};	
			
			/* Register this component */
			Alfresco.util.ComponentManager.register(this);
			//this.showRanking();
		},
  
		/**
		 * Show ranking
		 * @method showRanking
		 */
		showRanking: function GWI_RunRanking_showRanking(runType)
		{
			// Hide the existing content
			Dom.setStyle(this.bodyContainer, "display", "none");
			// Make an AJAX request to the Tag Service REST API
			var wsUrl = "/be/gwi/components/dashlets/run-challenge/ranking?htmlid="+this.id;
			if (runType != null){
	        	wsUrl += "&runType=" + runType;
	        }
			Alfresco.util.Ajax.jsonGet(
			{
				url: Alfresco.constants.URL_SERVICECONTEXT + wsUrl,
				successCallback:
				{
					fn: this.onShowRankingSuccess,
					scope: this
				},
				failureCallback:
				{
					fn: this.onShowRankingFailed,
					scope: this
				},
				scope: this,
				noReloadOnAuthFailure: true
			});
		},
      
		/**
		 * Show ranking retrieved successfully
		 * @method onShowRankingSuccess
		 * @param p_response {object} Response object from request
		 */
		onShowRankingSuccess: function GWI_RunRanking_onShowRankingSuccess(p_response)
		{	
			var detailsHtml = p_response.serverResponse.responseText;
			this.bodyContainer.innerHTML = detailsHtml;
			this.bindListeners();
			// Fade the new content in
			Alfresco.util.Anim.fadeIn(this.bodyContainer);
		},

		/**
		 * Show ranking request failed
		 * @method onShowRankingFailed
		 */
		onShowRankingFailed: function GWI_RunRanking_onShowRankingFailed()
		{
			this.bodyContainer.innerHTML = this.msg("showRanking-failed");
			Alfresco.util.Anim.fadeIn(this.bodyContainer);
		}
		,
	    /**
		 * Bind listeners
		 * @method bindListeners
		 */
		bindListeners: function GWI_RunRanking_bindListeners()
		{
			// Bind the listeners
			var showUserDetailsActions = Dom.getElementsByClassName("showUserDetails");
			var showUserHistoryActions = Dom.getElementsByClassName("showUserHistory");
			
			Event.addListener(showUserDetailsActions, "click", function(e) {
				var username = e.currentTarget.parentNode.parentNode.attributes.username.value;
				this.publisher.sendMessage(username);		
			}, this, true);
			Event.addListener(showUserHistoryActions, "click", function(e) {
				var username = e.currentTarget.parentNode.parentNode.attributes.username.value;
				this.publisher.showHistory(username);		
			}, this, true);
		}
	});
})();
